package de.baumann.browser.browser;

import android.net.Uri;
import android.os.Message;
import android.view.View;

import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import de.baumann.browser.web.OdinWebView;

public class OdinWebChromeClient extends WebChromeClient {
    private final OdinWebView ninjaWebView;

    public OdinWebChromeClient(OdinWebView ninjaWebView) {
        super();
        this.ninjaWebView = ninjaWebView;
    }

    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
        return isUserGesture;
    }

    @Override
    public void onProgressChanged(WebView view, int progress) {
        super.onProgressChanged(view, progress);
        if (ninjaWebView != null) {
            ninjaWebView.onProgressChanged(progress);
        }
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);
        if (ninjaWebView != null) {
            ninjaWebView.onReceivedTitle(title, view.getUrl());
        }
    }

    @Override
    public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
        if (ninjaWebView != null) {
            ninjaWebView.showCustomView(view, customViewCallback);
        }
        super.onShowCustomView(view, customViewCallback);
    }

    /*  @Override
    public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
        if (ninjaWebView != null) {
            ninjaWebView.showCustomView(view, callback);
        }
        super.onShowCustomView(view, callback);
    }*/

    @Override
    public void onHideCustomView() {
        if (ninjaWebView != null) {
            ninjaWebView.hideCustomView();
        }
        super.onHideCustomView();
    }

    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
        return true;
    }

    @Override
    public void onGeolocationPermissionsShowPrompt(String s, GeolocationPermissionsCallback geolocationPermissionsCallback) {
        geolocationPermissionsCallback.invoke(s, true, false);
        super.onGeolocationPermissionsShowPrompt(s, geolocationPermissionsCallback);
    }
}
